from datetime import datetime

class SummaryGenerator:
    def __init__(self, writer, summary_sheet_name="测试汇总"):
        self.writer = writer
        self.summary_sheet_name = summary_sheet_name

    def generate_summary(self, summary_data):
        """生成测试汇总数据到Excel"""
        self.writer.set_sheet(self.summary_sheet_name)
        headers = ["Sheet名称", "用例总数", "通过用例", "失败用例", "执行时间"]
        for col, header in enumerate(headers):
            self.writer.write(0, col, header, color="3")
        row = 1
        for sheet, data in summary_data.items():
            self.writer.write(row, 0, sheet)
            self.writer.write(row, 1, data['case_total'])
            self.writer.write(row, 2, data['case_pass'])
            self.writer.write(row, 3, data['case_fail'])
            self.writer.write(row, 4, datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            row += 1

    def _adjust_column_widths(self):
        widths = [20, 10, 10, 10, 18]
        for idx, width in enumerate(widths):
            self.writer.set_column_width(idx, width)